<?php


namespace app\admin\model;


use app\admin\validate\ServiceDepositValidate;
use think\Db;

class ServiceDepositModel extends BaseModel
{
    protected $name='service_deposit';

    protected $pk='id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public function doAddData($param=[],$mw=[]){
        $service_id = $param['service_id'];//服务管理的寄出的id
        $deceased_id = $param['deceased_id'];
        $submit_status = $param['submit_status'];


        $validate = new ServiceDepositValidate();
        $result=[];
        if(!$validate->scene('edit_jc')->check($param)) {
            $this->error = $validate->getError();
            return false;
        }


        $this->startTrans();
        try {
            if ($service_id) {
                // 先判断该逝者信息是否已提交审核
                $apinfo = ApprovalModel::where(['form_id' => $service_id, 'form_type' => 2, 'detail_type' => 8, 'approval_status' => '1'])->find();
                if (!empty($apinfo)) {
                    $this->error = "寄出信息正在审核中，不允许修改内容！";
                    $this->rollback();
                    return false;
                }
                $services = array();
                $services['deposit_id'] = $param['deposit_id'];
                $services['tomb_id'] = $param['tomb_id'];
                $services['tomb_num'] = $param['tomb_num'];
                $services['customer_id'] = $param['customer_id'];
                $services['tomb_address'] = $mw['tomb_region'] . $mw['tomb_address'];
                $services['dealer_user'] = $param['buyer_name']; // 办理人
                $services['dealer_sex'] = $param['buyer_sex']; // 办理人
                $services['dealer_phone'] = $param['buyer_phone']; // 办理人
                $services['dealer_cart_num'] = $param['cart_num']; // 办理人
                $services['dealer_cart_type'] = $param['cart_type']; // 办理人
                $services['dealer_address'] = $param['cart_address']; // 办理人
                $services['relationship'] = $param['relationship']; // 办理人
                $services['url_cart_file'] = $param['file_cart']; // 办理人
                $services['url_file_original'] = $param['url_file_original'];
                $services['handle_id'] = cmf_get_current_admin_id();
                $services['handle_name'] = $param['handle_name'];
                $services['buyer_name'] = $mw['buyer_name'];;
                $services['dead_name'] = $param['dead_name'];
                $services['submit_status'] = $param['submit_status'];
                $services['move_time'] = strtotime($param['move_time']);;
                $services['type'] = 1;
                $this->update($services, ['id' => $service_id]);
                $death['servicedeposit_id'] = $service_id;
                $death['move_status'] = '1';
                ArchivesDeceasedModel::update($death, ['deceased_id' => $deceased_id]);
                if ($submit_status == 2) {
                    // 如果是提交审核的状态保存审核表
                    ApprovalModel::addSave(2, 8, $service_id);
                } else if ($submit_status == 4) {
                    // 如果被审核驳回的时候
                    $appdata = Db::name('approval')->where(['form_id' => $service_id, 'form_type' => 2, 'detail_type' => 8, 'approval_status' => '1'])->find();
                    $appdata['approval_status'] = '1';
                    $appdata['approval_desc'] = '';
                    $appdata['approval_user'] = '';
                    $appdata['approval_time'] = '';
                    $appdata['apply_time'] = time();
                    $appdata['apply_user'] = cmf_get_current_admin_id();
                    Db::name("approval")->update($appdata);
                }
            } else {
                $services = array();
                $services['deposit_id'] = $param['deposit_id'];
                $services['tomb_id'] = $param['tomb_id'];
                $services['tomb_num'] = $param['tomb_num'];
                $services['customer_id'] = $param['customer_id'];
                $services['tomb_address'] = $mw['tomb_region'] . $mw['tomb_address'];
                $services['dealer_user'] = $param['buyer_name']; // 办理人
                $services['dealer_sex'] = $param['buyer_sex']; // 办理人
                $services['dealer_phone'] = $param['buyer_phone']; // 办理人
                $services['dealer_cart_num'] = $param['cart_num']; // 办理人
                $services['dealer_cart_type'] = $param['cart_type']; // 办理人
                $services['dealer_address'] = $param['cart_address']; // 办理人
                $services['relationship'] = $param['relationship']; // 办理人
                $services['url_cart_file'] = $param['file_cart']; // 办理人
                $services['url_file_original'] = $param['url_file_original'];
                $services['handle_id'] = cmf_get_current_admin_id();
                $services['handle_name'] = $param['handle_name'];
                $services['buyer_name'] = $mw['buyer_name'];;
                $services['dead_name'] = $param['dead_name'];
                $services['service_status'] = '1';
                $services['add_time'] = time();
                $services['add_user'] = cmf_get_current_admin_id();
                $services['move_time'] = strtotime($param['move_time']);
                $services['type'] = 1;

                $serinsert_id = $this->insertGetId($services);
                $death['servicedeposit_id'] = $serinsert_id;
                $death['move_status'] = '1';
                ArchivesDeceasedModel::update($death, ['deceased_id' => $deceased_id]);
                if ($submit_status == '2') {
                    // 如果是提交审核的状态保存审核表
                    ApprovalModel::addSave(2, 8, $serinsert_id);
                }
            }
            // 发送短信
            if(isset($param['is_send'])){
                $iSenf =  sendNoticeSms(2,$param['buyer_phone'],['move_time'=>$param['move_time']],$errorMsg);
                if ($iSenf==false){
                    return $this->error($errorMsg);
                }
            }

            $this->commit();
            return true;
        }catch(\Exception $e){
            $this->error='操作失败';
            $this->rollback();
            return false;
        }
    }

}